<!--
    -  Copyright (c) 2018-2025, haohanwork.com All rights reserved.
  -->
<template>
    <div class="execution">
        <basic-container>
            <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @selection-change="selectionChange">
                <template slot="menuRight">
                    <el-button type="primary" icon="el-icon-edit" size="small" @click="dialogVisible = true">转为采购单
                    </el-button>
                </template>
                <template slot-scope="scope" slot="menu">
                    <el-button type="text"
                               size="small"
                               plain
                               @click="jumpToDetail(scope.row)">详情
                    </el-button>
                </template>
            </avue-crud>
            <el-dialog
                    title="商城订单转为采购单"
                    :visible.sync="dialogVisible"
                    width="60%">
                <el-form ref="form">
                    <el-row>
                        <el-col :span="8">
                            <el-form-item label="送货日期">
                                <el-date-picker
                                        format="yyyy-MM-dd"
                                        value-format="yyyy-MM-dd"
                                        v-model="deliveryDate"
                                        type="date"
                                        placeholder="选择日期">
                                </el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="配送批次">
                                <el-select v-model="buySeq" placeholder="请选择">
                                    <el-option
                                            v-for="item in buySeqOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item label="配送类型">
                                <el-select v-model="deliveryType" placeholder="请选择">
                                    <el-option
                                            v-for="item in deliveryTypeOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <span slot="footer">
                <el-button type="primary"
                           :loading="btnLoading"
                           round
                           @click="createPurchaseOrder">确认
                </el-button>
            </span>
            </el-dialog>
        </basic-container>
    </div>
</template>

<script>
    import {fetchList, transBuyOrder} from '../../../api/salec/storeorder'
    import {tableOption} from '../../../const/crud/salec/storeorder'
    import {fetchDict} from '../../../util/util';

    export default {
        name: 'storeorder',
        data() {
            return {
                tableData: [],
                page: {
                    total: 0, // 总页数
                    currentPage: 1, // 当前页数
                    pageSize: 20 // 每页显示多少条
                },
                tableLoading: false,
                tableOption: tableOption,
                orderData: [],
                deliveryDate: '',
                buySeq: '0',
                deliveryType: '9',
                buySeqOptions: [],
                deliveryTypeOptions: [],
                dialogVisible: false,
                btnLoading: false
            }
        },
        created() {
            fetchDict("scm_delivery_seq", this.buySeqOptions);
            fetchDict("scm_delivery_type", this.deliveryTypeOptions);
        },
        mounted() {
            this.tableOption.selectable = this.selectRow;
        },
        methods: {
            /**
             * 可选择行处理
             */
            selectRow(row, index) {
                // 需已支付
                return row.paid === 1 && row.isScm === '0';
            },
            getList(page, params) {
                this.tableLoading = true
                fetchList(Object.assign({
                    current: page.currentPage,
                    size: page.pageSize
                }, params)).then(response => {
                    this.tableData = response.data.data.records
                    this.page.total = response.data.data.total
                    this.tableLoading = false
                })
            },
            /**
             * 搜索回调
             */
            searchChange(form, done) {
                this.getList(this.page, form);
                done();
            },
            /**
             * 刷新回调
             */
            refreshChange(info) {
                this.getList(this.page, this.filterForm(info.searchForm));
            },
            /**
             * 选择数据
             * @param list
             */
            selectionChange(list) {
                this.orderData = list
            },
            createPurchaseOrder() {
                this.btnLoading = true;
                let params = {
                    orderIdList: this.orderData.map(item => item.orderId),
                    buySeq: this.buySeq,
                    deliveryDate: this.deliveryDate,
                    deliveryType: this.deliveryType
                }
                let flag = true;
                if (!(params.buySeq && params.deliveryDate && params.deliveryType)) {
                    this.$message.warning("请选择转换后的时间、批次、送货方式")
                } else if (params.orderIdList.length === 0) {
                    this.$message.warning("请选择需转换的订单")
                } else {
                    flag = false;
                }
                if (flag) {
                    this.btnLoading = false;
                    return;
                }
                transBuyOrder(params).then(res => {
                    this.$notify({
                        title: "商城订单转采购单:",
                        dangerouslyUseHTMLString: true,
                        message: res.data.data.replace(/[|]/ig, "<br/><br/>"),
                        duration: 0
                    });
                }).finally(() => {
                    this.btnLoading = false;
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
